package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.bean.TagClass;
import com.afkt.project.util.ToastUtil;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.widget.ui.TagComponentView;
import com.dev.widget.ui.tag.DeConstants;
import com.dev.widget.ui.tag.Tag;
import com.dev.widget.ui.tag.TagComponent;
import com.dev.widget.util.widget.TagView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;

public class WrapSelectSlice extends BaseActivity {

    private static final float RADIUS = 10f;

    private static final int HARFVALUE = 2;

    private TagComponentView tagComponentView;

    private TextField editText;

    private ArrayList<TagClass> tagList;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_ability_wrap_select;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        initViewContent();
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void onActive() {
        super.onActive();
        tagComponentView.setTagLayoutListener((mInflater, tagWidth, position) -> {
            Component c= mInflater.parse(ResourceTable.Layout_wrap_item, null, true);
            Image img=(Image)c.findComponentById(ResourceTable.Id_img_item);
            int with=0;
            if(position%3==0){
                img.setPixelMap(ResourceTable.Media_icon_image);
                with=64;
            }else if(position%2==0){
                img.setPixelMap(ResourceTable.Media_Lark20210330);
                with=70;
            }else{
                img.setPixelMap(ResourceTable.Media_cat);
                with=200;
            }
            return new TagComponent(c,with);
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("换行监听 View");
        bar_Layout.addComponent(bar);
        Component view = QuickHelper.get(new BaseTextView(this))
                .setText("刷新")
                .setBold()
                .setTextColor(ResourceUtils.getColor(ResourceTable.Color_red))
                .setTextSizeBySp(15.0f)
                .setPaddingLeft(30)
                .setPaddingRight(30)
                .setOnClicks((component) -> {
                    initViewContent();
                })
                .getView();
        bar.addComponent(view);


    }


    private void initViewContent() {

        editText = (TextField) findComponentById(ResourceTable.Id_editText);
        tagComponentView = (TagComponentView) findComponentById(ResourceTable.Id_tag_view_group);

        prepareTags();
        editText.addTextObserver((s, i, i1, i2) -> setTags(s));
        editText.setText("M");
        tagComponentView.setOnTagClickListener((tag, position) -> editText.setText(tag.getText()));
    }

    private void prepareTags() {
        tagList = new ArrayList<>();
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(DeConstants.COUNTRIES).getAsJsonArray();
        Gson gson = new Gson();
        for (JsonElement tag : jsonArray) {
            TagClass tagClass = gson.fromJson(tag, TagClass.class);
            tagList.add(new TagClass(tagClass.getSinif(), tagClass.getName()));
        }
    }

    private void setTags(CharSequence cs) {
        /**
         * for empty edittext
         */
        if (cs.toString().equals("")) {
            tagComponentView.addTags(new ArrayList<Tag>());
            return;
        }

        String text = cs.toString();
        ArrayList<Tag> tags = new ArrayList<>();
        Tag tag;
        for (int index = 0; index < tagList.size(); index++) {
            if (tagList.get(index).getName().toLowerCase().startsWith(text.toLowerCase())) {
                tag = new Tag(tagList.get(index).getName());
                tag.setRadius(RADIUS);
                tag.setLayoutColor(Color.getIntColor(tagList.get(index).getColor()));
                if (index % HARFVALUE == 0) {
                    tag.setDeletable(true);
                }
                tags.add(tag);
            }
        }
        tagComponentView.addTags(tags);
    }

}
